<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'base.item.class.php');

class MenuContentBase extends BaseItemBase {
	
	//constructor
	function MenuContentBase() {
		parent::BaseItemBase();
		
		$this->item_link = true;
		$this->item_link_owner = true;
		$this->table = 'pp_menus';
		$this->prefix = 'menus_';
		
		$this->search_fields = array('name');
		$this->sort_fields = array('sort_order+', 'name+');
		
		$this->filter_fields = array('cid:=', 'tid:=', 'type:=', 'class:=', 'style:=');
		
		//
		$this->table_content = 'pp_contents';
		$this->prefix_content = 'contents_';
	}
	
	//methods
	function syncItems() {
		//
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/content/content.class.php');
		$mObj = new ContentBase();
		
		//
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/content/content.category.class.php');
		$mcObj = new ContentCategoryBase();
		
		//
		$query = "select * from pp_menus where " . $this->_query_owner('menus_owner');
		$result = $this->adb->query($query);
		while($row = $this->adb->fetch_array($result)) {
			if( strpos($row['menus_link'], '/') !== false ) {
				continue;
			}
			$sql = "select count(*) as count from pp_contents where contents_app1 = 1 and contents_app2 = '" . $row['menus_id'] . "'";
			$count = $this->adb->getCount($sql);
			if( $count > 0 ) {
				continue;
			}
			$data_array = array();
			$data_array['_name'] = $row['menus_name'];
			$data_array['_subject'] = $row['menus_name'];
			$data_array['_link'] = $row['menus_link'];
			$data_array['_app1'] = '1';
			$data_array['_app2'] = $row['menus_id'];
			$data_array['_app3'] = (($row['menus_parent'] == 0) ? 1 : 0);
			$data_array['_cid'] = $this->_get_menu_root_parent($row['menus_id'], $row['menus_parent']);
			
			$mObj->addItem($data_array);
		}
		
		//
		$i = 0;
		$query = "select * from pp_menus where menus_parent = 0 and " . $this->_query_owner('menus_owner');
		$result = $this->adb->query($query);
		while($row = $this->adb->fetch_array($result)) {
			if( strpos($row['menus_link'], '/') !== false ) {
				continue;
			}
			$sql = "select count(*) as count from pp_contents_categories where categories_type = 'menu:" . $row['menus_id'] . "'";
			$count = $this->adb->getCount($sql);
			if( $count > 0 ) {
				continue;
			}
			$i++;
			$data_array = array();
			$data_array['categories_type'] = 'menu:' . $row['menus_id'];
			$data_array['categories_name'] = $row['menus_name'];
			$data_array['categories_link'] = $row['menus_link'];
			$data_array['categories_description'] = $row['menus_description'];
			$data_array['categories_sort_order'] = $i;
			
			$mcObj->addCategory($data_array);
		}
		
		return true;
	}
	
	function _get_menu_root_parent($id, $p = 0) {
		if( $p == 0 ) {
			return $id;	
		}
		$query = "select * from pp_menus where menus_id = '" . $p . "'";
		$row = $this->adb->getRow($query);
		
		return $this->_get_menu_root_parent($row['menus_id'], $row['menus_parent']);
	}
}
//
?>